/*
 * 文件名：DES.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-6
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.security;


import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


/**
 * DES加密解密
 * <DES加密和解密过程中，密钥长度都必须是8的倍数>
 * @author will_awoke
 * @version 2014-8-6
 * @see DES
 * @since
 */
public class DES
{

    /**
     * log 
     */
    private static final Logger log = Logger.getLogger(DES.class);

    /**
     * default key
     */
    private static final String DEFAULT_DES_PWD_KEY = "ue23sp1pwcvs58ol";

    /**
     * DES
     */
    private final static String DES = "DES";

    /**
     * Description 根据键值进行加密
     * @param plaintext 
     * @return
     */
    public static String encrypt(String plaintext)
    {
        return encrypt(plaintext, DEFAULT_DES_PWD_KEY);
    }

    /**
     * Description 根据键值进行解密
     * @param ciphertext
     * @return
     */
    public static String decrypt(String ciphertext)
    {
        return decrypt(ciphertext, DEFAULT_DES_PWD_KEY);
    }

    /**
     * Description 根据键值进行加密
     * @param plaintext 
     * @param key  加密键byte数组
     * @return
     */
    public static String encrypt(String plaintext, String key)
    {
        byte[] bt = encrypt(plaintext.getBytes(), key.getBytes());
        if (bt == null)
        {
            return null;
        }
        return new BASE64Encoder().encode(bt);
    }

    /**
     * Description 根据键值进行解密
     * @param ciphertext
     * @param key  加密键byte数组
     * @return
     */
    public static String decrypt(String ciphertext, String key)
    {
        try
        {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bt = decrypt(decoder.decodeBuffer(ciphertext), key.getBytes());
            if (bt == null)
            {
                return null;
            }
            return new String(bt);
        }
        catch (IOException e)
        {
            log.error(e);
        }
        return null;
    }

    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     */
    private static byte[] encrypt(byte[] data, byte[] key)
    {
        try
        {
            // 生成一个可信任的随机数源
            SecureRandom sr = new SecureRandom();

            // 从原始密钥数据创建DESKeySpec对象
            DESKeySpec dks = new DESKeySpec(key);

            // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
            SecretKey securekey = keyFactory.generateSecret(dks);

            // Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance(DES);

            // 用密钥初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

            return cipher.doFinal(data);
        }
        catch (Exception e)
        {
            log.error(e);
        }
        return null;
    }

    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key)
    {
        try
        {
            // 生成一个可信任的随机数源
            SecureRandom sr = new SecureRandom();

            // 从原始密钥数据创建DESKeySpec对象
            DESKeySpec dks = new DESKeySpec(key);

            // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
            SecretKey securekey = keyFactory.generateSecret(dks);

            // Cipher对象实际完成解密操作
            Cipher cipher = Cipher.getInstance(DES);

            // 用密钥初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

            return cipher.doFinal(data);
        }
        catch (Exception e)
        {
            log.error(e);
        }
        return null;
    }

    public static void main(String[] args)
    {
        log.info(encrypt("liuyuhua0000", DEFAULT_DES_PWD_KEY));
        log.info(decrypt("v1wQRtcLkomVsYx83neV+Q==", DEFAULT_DES_PWD_KEY));
        log.info(encrypt("xuzhifang32334242", DEFAULT_DES_PWD_KEY));
    }
}
